<template>
    <div class="front bg-white">
        <div class="form">
            <el-form :model="formData" label-position="left" label-width="150px" status-icon ref="formRef" class="demo-ruleForm">
                <el-form-item label="启用会计年度：" prop="startAccountYear">
                    <el-select v-model="formData.startAccountYear" placeholder="选择启用会计年度" style="width: 100% !important">
                        <el-option v-for="(item, index) in startAccountYear" :key="index" :label="item" :value="item"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="启用会计期间：" prop="startAccountPeriod">
                    <el-select v-model="formData.startAccountPeriod" placeholder="选择启用会计区间" style="width: 100% !important">
                        <el-option v-for="(item, index) in startAccountPeriod" :key="index" :label="item" :value="item"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="当前会计年度：" prop="currentAccountYear">
                    <el-input type="text" v-model="formData.currentAccountYear" autocomplete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="当前会计期间：" prop="currentAccountPeriod">
                    <el-input type="text" v-model="formData.currentAccountPeriod" autocomplete="off" disabled></el-input>
                </el-form-item>
                <el-form-item style="margin-bottom: 15px !important">
                    <span class="textTip"　v-if="isOpen" style="color: green;">财务系统已经启用</span>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" size="small" @click="save('formRef')">保存</el-button>
                    <el-button type="danger" size="small" @click="close">关闭</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
/*
*  author: 'iissoftware'
*  description: 财务参数组件
*/
export default {
    data() {
        return {
            isOpen: true,
            formData: {
                startAccountYear: '',
                startAccountPeriod: '',
                currentAccountYear: (new Date().getFullYear()).toString(),
                currentAccountPeriod: new Date().getMonth() + 1
            },
            startAccountYear: [],       //启用会计年度
            startAccountPeriod: [],      //启用会计区间
            systemId: this.$store.state.systemId
        }
    },
    methods: {
        getData() {             //获取财务参数
            this.$http.get(this.$url + 'systemParameters/selectSysFinancialParameterByUser').then(res => {
                if(res.data.code == 20001) {
                    if(res.data.data) {
                        this.formData['startAccountYear'] = String(res.data.data['startAccountYear']);
                        this.formData['startAccountPeriod'] = String(res.data.data['startAccountPeriod']);
                    } else {
                        this.formData['startAccountYear'] = (new Date().getFullYear()).toString();
                        this.formData['startAccountPeriod'] = (new Date().getMonth() + 1).toString();
                    }
                    this.defaultYear(this.formData['startAccountYear']);
                } else {
                    this.formData['startAccountYear'] = (new Date().getFullYear()).toString();
                    this.formData['startAccountPeriod'] = (new Date().getMonth() + 1).toString();
                    this.defaultYear(this.formData['startAccountYear']);
                }
            });
        },
        save(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if(!this.systemId) {
                        this.$message.error('先添加系统参数');
                        return false;
                    }
                    if(isNaN(this.formData['startAccountYear'])) {
                        this.$message({message: '启用会计年度不是4位数字', duration: 1500, type: 'warning'});
                        return false;
                    }
                    if(this.formData['startAccountYear'].length != 4) {
                        this.$message({message: '启用会计年度长度只能是4位数字', duration: 1500, type: 'warning'});
                        return false;
                    }
                    if(isNaN(this.formData['startAccountPeriod'])) {
                        this.$message({message: '启用会计区间不是一个数字', duration: 1500, type: 'warning'});
                        return false;
                    }
                    if(this.formData['startAccountPeriod'] < 1 || this.formData['startAccountPeriod'] > 12) {
                        this.$message({message: '启用会计区间输入不合法', duration: 1500, type: 'warning'});
                        return false;
                    }
                    let $url;
                    if(this.formData['id']) {
                        //修改
                        delete this.formData['createTime'];
                        delete this.formData['updateTime'];
                        delete this.formData['systemId'];
                        $url = this.$url + 'systemParameters/updateSysFinancialParameter';
                    } else {
                        //新增
                        this.formData['userName'] = this.userName;
                        $url = this.$url + 'systemParameters/insertSysFinancialParameter'
                    }
                    this.$http({
                        url: $url,
                        method: 'post',
                        params: this.formData
                    }).then(res => {
                        if(res.data.code == 20001) {
                            this.$message({message: '保存成功', duration: 1000, type: 'success'});
                        } else {
                            this.$message.error(res.data.msg);
                        }
                    });
                }
            });
        },
        defaultYear(year) {
            let startYear = year - 100,
                endYear = parseInt(year) + 100;
            for(let i = startYear; i <= endYear; i++) {
                this.startAccountYear.push(i.toString());
            }
        },
        defaultMonth() {
            for(let i = 1; i <= 12; i++) {
                this.startAccountPeriod.push(i.toString());
            }
        },
        close() {           //关闭新增弹窗
            let index = parent.layer.getFrameIndex(window.name);    //先得到当前iframe层的索引
            parent.layer.close(index);
        },
        enterKey(lastRef, nextRef) {        //回车键换行
            if(lastRef.target.value) {
                this.$refs[nextRef].focus();
            }
        }
    },
    created() {
        //判断启用系统是否是结束初始化
        let enableStatus = Number(localStorage.getItem('setRadio'));
        (enableStatus && enableStatus === 1) ? this.isOpen = true : this.isOpen = false;
        this.getData();
        this.defaultMonth();
    }
}
</script>
<style scoped>
    .form {
        width: 400px;
        margin: 0 auto;
        padding-top: 50px;
    }
    .textTip {
        font-size: 16px;
    }
    .open {
        color: #008000;
    }
</style>